package com.jeomo.masterdata.starter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.common.core.constant.SearchParam;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.masterdata.starter.entity.Goods;
import com.jeomo.masterdata.starter.mapper.GoodsMapper;
import com.jeomo.masterdata.starter.service.IGoodsService;
import com.jeomo.masterdata.starter.util.CommonUtil;
import com.jeomo.shiro.dto.LoginInfo;
import com.jeomo.shiro.util.LoginSessionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: qbt
 * @Date: 2020/3/8 15:35
 * @Version 1.0
 */
@Service
public class GoodsServiceImpl extends BaseServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public int create(Goods goods) {
        LoginInfo currentLoginInfo = LoginSessionUtil.getCurrentLoginInfo();
        goods.setOrgCode(currentLoginInfo.getCurrentOrgCode());
        goods.setTopOrgCode(currentLoginInfo.getTopOrgCode());
        String recentlyCode = goodsMapper.queryRecentlyCode(goods.getMerchantCode());
        String code = CommonUtil.spliceCode(goods.getMerchantCode(), recentlyCode);
        goods.setCode(code);
        return goodsMapper.insert(goods);
    }

    @Override
    public List<Goods> selectGoodsByMerchantCodes(String merchantCode) {

        return goodsMapper.selectGoodsByMerchantCodes(merchantCode);
    }

    @Override
    public PageResponseDto<Goods> pageGoodsByMerchantCodes(Page<Goods> page, List<String> merchantCode) {
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.in("merchant_code", merchantCode);
        goodsQueryWrapper.eq("start_flag", true);
        Page<Goods> goodsPage = goodsMapper.selectPage(page, goodsQueryWrapper);
        PageResponseDto<Goods> dto = new PageResponseDto<>();
        dto.setItems(goodsPage.getRecords());
        dto.setTotal(goodsPage.getTotal());
        return dto;
    }

    @Override
    public List<Goods> selectGoodsByGoodsTypeCodes(List<String> GoodsTypeCodes) {

        return goodsMapper.selectGoodsByGoodsTypeCodes(GoodsTypeCodes);
    }

    @Override
    public List<Goods> selectGoodsByTobCodes(List<String> tobCodes) {

        return goodsMapper.selectGoodsByTobCodes(tobCodes);
    }

    @Override
    public PageResponseDto pageSearch(PageQueryDto dto) {
        //1. 处理查询参数
        Map<String, Object> searchParams = dto.getSearchParams();
        searchParams.put(SearchParam.SEARCH_EQ + "org_code", LoginSessionUtil.getCurrentLoginInfo().getCurrentOrgCode());
        return super.pageSearch(dto);
    }

    @Override
    public PageResponseDto<Goods> pageSearchByMerchantCodes(PageQueryDto dto, List<String> merchantCodes) {
        //1. 处理查询参数
        Map<String, Object> searchParams = dto.getSearchParams();
        searchParams.put(SearchParam.SEARCH_EQ + "org_code", "001");
        searchParams.put(SearchParam.SEARCH_EQ + "start_flag", true);
        searchParams.put(SearchParam.SEARCH_IN + "merchant_code", merchantCodes);
        return super.pageSearch(dto);
    }
}
